package com.example.yls.demoa;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                showSimpleDialog();
            }
        });
        findViewById(R.id.btn_hobby).setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                showHobbyDialog();
            }

            private void showHobbyDialog() {
            final String[] hobby = {"篮球","足球","棒球","睡觉","乒乓球"};
            final boolean isChecked[] = new boolean[6];
            new AlertDialog.Builder(MainActivity.this)
            .setIcon(R.mipmap.ic_launcher)
            .setTitle("请选择你的爱好")
            .setMultiChoiceItems(hobby, null, new DialogInterface.OnMultiChoiceClickListener() {
               @Override
               public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                   isChecked[i] = b;

                   if(b){
                       Toast.makeText(MainActivity.this,
                               "你添加了"+hobby[i],Toast.LENGTH_SHORT).show();
                   }else {
                       Toast.makeText(MainActivity.this,
                               "你取消了"+hobby[i],Toast.LENGTH_SHORT).show();
                   }
               }
           })
           .setPositiveButton("确定", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialogInterface, int i) {
      StringBuffer buffer = new StringBuffer();

      for(int j =0; j<isChecked.length; j++){
          if(isChecked[j]){
              buffer.append(hobby[j]);
          }
      }
      Toast.makeText(MainActivity.this,"你的爱好有"+ buffer,Toast.LENGTH_SHORT).show();
               }
           })
           .create()
           .show();

            }
        });
        findViewById(R.id.btn_deleted).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showChooseDialog();
            }
        });
        
      findViewById(R.id.btn_sex).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              showSexDialog();
          }


          private void showSexDialog() {
              AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
              builder.setIcon(R.drawable.ic_launcher_background);
              builder.setTitle("请选择性别");
              final String[] sex = {"男", "女", "未知性别"};
              builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener()
              {
                  @Override
                  public void onClick(DialogInterface dialog, int which)
                  {
                      Toast.makeText(MainActivity.this, "性别为：" + sex[which], Toast.LENGTH_SHORT).show();
                  }
              });
              builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
              {
                  @Override
                  public void onClick(DialogInterface dialog, int which)
                  {

                  }
              });
              builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
              {
                  @Override
                  public void onClick(DialogInterface dialog, int which)
                  {

                  }
              });
              builder.show();
          }
      });

        findViewById(R.id.btn_adress).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showWhereDialog();
            }

            public void showWhereDialog() {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setIcon(R.drawable.ic_launcher_background);
                    builder.setTitle("please choose other one city");
                    final String[] cities = {"广州","茂名","化州","潭塘","拉萨"};
                    builder.setItems(cities, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MainActivity.this,
                                    "选择的城市为：" + cities[i],
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                builder.show();


            }
        });
    }


    private void showChooseDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.drawable.ic_launcher_background);
        builder.setTitle("弹出警告框");
        builder.setMessage("确定要删除吗？");

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                Toast.makeText(MainActivity.this,
                        "positive: " + which,
                        Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("忽略", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                Toast.makeText(MainActivity.this,
                        "neutral: " + which,
                        Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                Toast.makeText(MainActivity.this,
                        "negative: " + which,
                        Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    private void showSimpleDialog(){


        new AlertDialog.Builder(MainActivity.this)
             .setIcon(R.mipmap.ic_launcher)
                .setTitle("提示")
               .setMessage("TO DAY IS YUOR BRIDATY")

        .setPositiveButton("确定", new DialogInterface.OnClickListener()
        {

            public void onClick(DialogInterface dialog, int which)
            {

            }
        })
                .create()
                .show();





    }

}
